package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.admin.web.util.UploadUtil;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/Strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IRegionService regionService;

    //大攻略的列表
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")StrategyQueryObject qo){
        qo.setPageSize(6);
        model.addAttribute("result", strategyService.getPageInfo(qo));
        model.addAttribute("regions", regionService.list());
        return "/Strategy/list";
    }

    //大攻略的新增和编辑操作
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(MultipartFile pic,Strategy strategy){
        JSONResult result = new JSONResult();

        try {
            if(pic!=null&&pic.getSize()>0){
              String imagePath = UploadUtil.upload(pic, UploadUtil.IMG_PATH+"upload");
              strategy.setCoverUrl(imagePath);
            }

            strategyService.saveOrUpdate(strategy);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }

        return result;
    }



}
